//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Collections;

using Mindscape.BackgroundMotion.Core.Properties;

namespace Mindscape.BackgroundMotion.Core.Utilities
{
  public static class Invariant
  {
    public static void ArgumentNotNull(object argument, string argumentName)
    {
      ArgumentNotEmpty(argumentName, "argumentName");

      if (argument == null)
      {
        throw new ArgumentNullException(argumentName);
      }
    }

    public static void ArgumentNotEmpty(Guid guid, string argumentName)
    {
      ArgumentNotEmpty(argumentName, "argumentName");

      if (guid == Guid.Empty)
      {
        throw new ArgumentOutOfRangeException(
          StringUtils.FormatCurrentCulture(Resources.GuidCannotBeEmpty, argumentName));
      }
    }

    public static void ArgumentNotEmpty(ICollection collection, string argumentName)
    {
      ArgumentNotNull(collection, "collection");
      ArgumentNotEmpty(argumentName, "argumentName");

      if (collection.Count == 0)
      {
        throw new ArgumentOutOfRangeException(
          StringUtils.FormatCurrentCulture(Resources.CollectionCannotBeEmpty, argumentName));
      }
    }

    public static void ArgumentNotEmpty<T>(System.Collections.Generic.ICollection<T> collection, string argumentName)
    {
      ArgumentNotNull(collection, "collection");
      ArgumentNotEmpty(argumentName, "argumentName");

      if (collection.Count == 0)
      {
        throw new ArgumentOutOfRangeException(
          StringUtils.FormatCurrentCulture(Resources.CollectionCannotBeEmpty, argumentName));
      }
    }

    public static void ArgumentNotEmpty(string argument, string argumentName)
    {
      if (string.IsNullOrEmpty(argumentName))
      {
        throw new ArgumentNullException(argumentName);
      }

      if (argument == null)
      {
        throw new ArgumentNullException(argumentName);
      }

      if (argument.Length == 0)
      {
        throw new ArgumentOutOfRangeException(
          StringUtils.FormatCurrentCulture(Resources.StringCannotBeEmpty, argumentName));
      }
    }

    public static void EnumValueIsDefined(Type enumType, object value, string argumentName)
    {
      ArgumentNotNull(value, "value");
      ArgumentNotEmpty(argumentName, "argumentName");

      if (Enum.IsDefined(enumType, value) == false)
      {
        throw new ArgumentException(
          StringUtils.FormatCurrentCulture(Resources.InvalidEnumValue, argumentName, enumType));
      }
    }

    public static void IsTrue(bool test)
    {
      if (!test)
      {
        throw new ApplicationException(Resources.InvariantWasFalse);
      }
    }
  }
}